/*******************************************************************************
 * Copyright (c) 2012 Clever Devices.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 * 	Clever Devices Interns - 
 * 	Alex Frankel, Andria Young, Kevin Thomas, Nicholas Culpepper, Taner Oztimurlenk, Thomas Volberg
 ******************************************************************************/
package com.framework.bustime_parser;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.framework.beans.Bus;
import com.framework.bustime.ArrivalsGrid;
import com.framework.cleverstop_tentative.R;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

/**
 * Override ItemizedOverlay particularly to override the OnTap method to
 * allow configuration of the dialog after selecting a map item
 * 
 * @author AFrankel
 *
 */
public class BusTimeItemizedOverlay extends ItemizedOverlay<OverlayItem>
		implements OnClickListener {

	public BusTimeItemizedOverlay(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		// TODO Auto-generated constructor stub
	}

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		// populate();
	}

	/**
	 * Populates the entire list of overlays once they have ALL been added.
	 * As opposed to populating the entire list after each individual item was added.
	 * Enable much better map performance (scrolling/zooming)
	 * 
	 */
	public void populateNow() {
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	/**
	 * Displays a dialog box with information about the Bus.  Currently displays
	 * the bus id and final destination (Button which takes you to arrivals grid)
	 */
	@Override
	protected boolean onTap(int index) {
		location = index;
		OverlayItem item = mOverlays.get(index);
		Dialog dialog = new Dialog(mContext);
		dialog.setContentView(R.layout.bustime_bus_detail);
		dialog.setTitle("Bus Details");

		TextView tvRouteId = (TextView) dialog.findViewById(R.id.routeID);
		tvRouteId.setText("  Bus ID: " + item.getTitle());
		// TextView tvDest = (TextView)
		// dialog.findViewById(R.id.destinationText);
		// tvDest.setText("  Destination: " + item.getSnippet());
		Button bArrivals = (Button) dialog.findViewById(R.id.bArrivalsGrid);
		bArrivals.setText(" Destination: " + item.getSnippet());
		bArrivals.setOnClickListener(this);

		dialog.show();

		// AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		// dialog.setTitle(item.getTitle());
		// dialog.setMessage(item.getSnippet());
		// dialog.show();
		return true;
	}

	private Context mContext;
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

	
	//starts the arrivals grid intent
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		OverlayItem item = mOverlays.get(location);
		Intent i = new Intent(mContext, ArrivalsGrid.class);
		Bundle b = new Bundle();
		b.putString("ID", item.getTitle());
		//i.putExtras(b);
		mContext.startActivity(i);

	}

	int location;
}
